##目标组成方式： 内核+设备+挂载+版本号+日期

#要编译内核名称： ZINO ZINO_I ZINO_II ZINO_RC ZINO_RC_PRO ZINO_	Mini ZINO_IO_TEST ZINO_LITE ZINO_PREMIUM ZINO_MOTOR_TEST ZINO_NP ZINO_DRONE ---必选！！！
BUILD_CORE = ZINO_PREMIUM
CORE_GROUP1 = ZINO ZINO_I
CORE_GROUP2 = ZINO_PREMIUM
CORE_GROUP=ZINO ZINO_I ZINO_II ZINO_RC ZINO_RC_PRO ZINO_Mini ZINO_IO_TEST ZINO_LITE ZINO_PREMIUM ZINO_MOTOR_TEST ZINO_NP ZINO_DRONE
#要编译的名称： 机甲之芯 天空之翼 遥控器出厂程序 引导程序 测试程序 其它 青春版 遥控器指挥 体感遥控器 ---必选！！！
BUILD_DEVICE = 天空之翼

#要编译的选项： 四驱 8mm水弹炮 足球套装 坦克 装甲战车01 挖掘机 机械臂---必选！！！
BUILD_EQUIPMENT = 四驱

SERIAL_PROT = COM9

#要编译的特性：自定义字符串，需要自己去根据程序那写
BUILD_FEATURE = NONE

BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 3
SUB_VERSION := 4
BUILD_NUMBER := 1
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)-$(BUILD_DATE)



ifeq ($(filter $(BUILD_CORE), $(CORE_GROUP)), $(BUILD_CORE))
$(info "Building CORE: $(BUILD_CORE)")
else
$(error "必须选择其中一个内核：$(CORE_GROUP"))
endif

ifeq ($(filter $(BUILD_CORE),$(CORE_GROUP1)),$(BUILD_CORE))
$(info "AT32F415 Group:$(BUILD_CORE)")
include ./system/at32f413.mk
else
$(info "AT32F413 Group:$(BUILD_CORE)")
include ./system/at32f413.mk
endif

include ./zino_fc.mk

TARGET = $(BUILD_CORE)
ROOT     		= ./
OBJECT_DIR      = $(ROOT)/obj/$(BUILD_CORE)
BIN_DIR  		= $(ROOT)/obj/$(BUILD_CORE)

TARGET_LINK = $(ROOT)/variants/$(BUILD_CORE)/$(BUILD_CORE)_LinkScript.ld
STARTUP_SRC = $(ROOT)/variants/$(BUILD_CORE)/$(BUILD_CORE)_STARTUP.s
BUILD_SRC = $(STARTUP_SRC) $(SYSTEM_SRC) $(VI5300_SRC) $(VL53L1X_SRC) $(ZINO_FC_SRC)
BUILD_INC = $(SYSTEM_INC) $(VI5300_INC) $(VL53L1X_INC) $(ZINO_FC_INC)
BUILD_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/,$(basename $(BUILD_SRC))))
BUILD_sys_LIBS = $(ROOT)/variants/$(BUILD_CORE)/$(BUILD_CORE)_sys.a
BUILD_FC_LIBS = $(ROOT)/variants/$(BUILD_CORE)/$(BUILD_CORE)_fc.a
TARGET_MAP      = $(OBJECT_DIR)/$(FIRMWARE_NAME).map
FIRMWARE_NAME   = $(BUILD_CORE)-$(BUILD_DEVICE)-$(BUILD_VERSION)
TARGET_BIN      = $(BIN_DIR)/$(FIRMWARE_NAME).bin
TARGET_HEX      = $(BIN_DIR)/$(FIRMWARE_NAME).hex
TARGET_ELF      = $(OBJECT_DIR)/$(FIRMWARE_NAME).elf

# Tool names
MIXLY_TOOL = D:/CODE/mixly_for_zino/Mixly_FOR__ZINO/arduino/hardware/UAVI_TECH/tools/arm/bin/
ARM_SDK_PREFIX ?= arm-none-eabi-
CROSS_CC    := $(ARM_SDK_PREFIX)gcc
CROSS_CXX   := $(ARM_SDK_PREFIX)g++
OBJCOPY     := $(ARM_SDK_PREFIX)objcopy
SIZE 		:= $(ARM_SDK_PREFIX)size
AR			:= $(ARM_SDK_PREFIX)ar

ARCH_FLAGS = $(TARGET_FLAGS) $(BUILD_DEFINE) \

CFLAGS = \
	$(MCU_FLAG) \
	$(ARCH_FLAGS) \
	-Os -mthumb -ffast-math -std=gnu11 -ffunction-sections -fdata-sections -Wdouble-promotion -Wunsafe-loop-optimizations \
	$(addprefix -I,$(BUILD_INC))
CXXFLAGS = \
	$(MCU_FLAG) \
	$(ARCH_FLAGS) \
	-Os \
	-ffast-math \
	-std=gnu++11 \
	-Wall \
	-ffunction-sections \
	-fdata-sections \
	-Wunsafe-loop-optimizations \
	-pedantic \
	-Wdouble-promotion \
	$(addprefix -I,$(BUILD_INC))

# $(info CFLAGS:$(CFLAGS))
ASFLAGS = \
	$(ARCH_FLAGS) \
	-x assembler-with-cpp \
	$(addprefix -I,$(BUILD_INC)) \
	-MMD -MP
LDFLAGS = \
	$(MCU_FLAG) \
	$(ARCH_FLAGS) \
	-specs=rdimon.specs \
	-flto \
	-fuse-linker-plugin \
	-Wl,--print-memory-usage \
	-T${TARGET_LINK} \
	-Wl,-Map,$(TARGET_MAP) \
	-Wl,--gc-sections \
	-Wl,--entry=Reset_Handler \
	-lc -u _printf_float


# -Wl,--no-warn-rwx-segments

#command tips visible
V1 = @
#command visible
V0 = @
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")

$(TARGET_BIN): $(TARGET_ELF)
	$(V0) echo "$(DATE_TIME) Generating binFile ... $@"
	$(OBJCOPY) -O binary $< $@

$(TARGET_ELF): $(BUILD_OBJS)
	$(V0) echo "$(DATE_TIME) Linking everyting together... $@"
	$(V0) $(CROSS_CC) -o $@ -Wl,--start-group $^ -Wl,--end-group $(LDFLAGS)
	$(V0) $(SIZE) $(TARGET_ELF)

# Compile
$(OBJECT_DIR)/%.o: %.c
	$(V0) echo "$(DATE_TIME) compiling c... $<"
	$(V1) mkdir -p $(dir $@)
	$(V1) $(CROSS_CC) $< -c -g -o $@ $(CFLAGS)

$(OBJECT_DIR)/%.o: %.cpp
	$(V0) echo "$(DATE_TIME) compiling cxx... $<"
	$(V1) mkdir -p $(dir $@)
	$(V1) $(CROSS_CXX) $< -c -g -o $@ $(CXXFLAGS)

# Assemble
$(OBJECT_DIR)/%.o: %.s
	$(V0) echo "$(DATE_TIME) Assembling... $<"
	$(V1) mkdir -p $(dir $@)
	$(V1) $(CROSS_CC) -c -g -o $@ $(ASFLAGS) $<

$(OBJECT_DIR)/%.o: %.S
	$(V0) echo "$(DATE_TIME) Assembling... $<"
	$(V1) mkdir -p $(dir $@)
	$(V1) $(CROSS_CC) -c -g -o $@ $(ASFLAGS) $<




binary:
	$(V0) $(MAKE) -j $(TARGET_BIN)

hex:
	$(V0) $(MAKE) -j $(TARGET_HEX)

targets:
	@echo "Target:      $(TARGET)"

clean:
	$(V0) echo "Cleaning $(TARGET)"
	$(V0) rm -rf $(OBJECT_DIR)
	$(V0) rm -rf $(CLEAN_ARTIFACTS)
	$(V0) echo "Cleaning $(TARGET) succeeded."

ISP_flash_$(TARGET): $(TARGET_BIN)
	$(ROOT)/UploadTool/ZINO_ISP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --baudrate 115200 --globalerase --loadfile $(TARGET_BIN) --write 0x08000000 PAUSE

UPDADTE_BOOTLOADER_$(TARGET): $(TARGET_BIN)
	$(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --board ZINO_IO_TEST --mcu AT32F415RCT7 --serialdev $(SERIAL_PROT) --id --version --loadfile $(TARGET_BIN) --updateBootloader --write 0x08001800
IAP_flash_$(TARGET): $(TARGET_BIN)
	chcp 65001
	$(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --board $(BUILD_CORE) --mcu $($(BUILD_CORE)_MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN) PAUSE
# $(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev COM3 --board $(BUILD_CORE) --mcu $($(BUILD_CORE)_MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN) PAUSE
IAP_flash10_$(TARGET): $(TARGET_BIN)
	chcp 65001
	$(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT2) --board $(BUILD_CORE) --mcu $($(BUILD_CORE)_MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN) PAUSE
IAP_flash11_$(TARGET): $(TARGET_BIN)
	chcp 65001
	$(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --board $(BUILD_CORE) --mcu $($(BUILD_CORE)_MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN) PAUSE
	$(ROOT)/UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT2) --board $(BUILD_CORE) --mcu $($(BUILD_CORE)_MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN) PAUSE
bintoarray: $(TARGET_BIN)
	$(V0) echo "Making $(TARGET) targetBin.c"
	$(ROOT)/BinToArray.exe $(TARGET_BIN) $(ROOT)/variants/$(BUILD_CORE)/$(FIRMWARE_NAME_EN).c
flash_isp: ISP_flash_$(TARGET)
flash_iap: IAP_flash_$(TARGET)
dl: IAP_flash_$(TARGET)
flash_iap10: IAP_flash10_$(TARGET)
dl3: IAP_flash10_$(TARGET)
flash_iap11: IAP_flash11_$(TARGET)
flash_bootloader: UPDADTE_BOOTLOADER_$(TARGET)
$(TARGET_OBJS) : Makefile

cleanIap:
	$(V0) echo "Cleaning $(IAP_LIB)"
	$(V0) rm -rf $(IAP_LIB)
	$(V0) rm -rf $(IAP_OBJS)
	$(V0) echo "done"

RebuildCore: $(TARGET_ELF) $(TARGET_DEPENDENS) $(TARGET_BIN)
